// 数据列表
import carSourceCard from './components/carSource-card.vue'
import carCard from '../../../components/card/car-card.vue' //车源列表
import beautyCard from './components/carbeauty-list-item.vue' //美护宝列表
import findPj from '@/components/acessory/listCard.vue' //列表卡片
import storeList from './components/store-list.vue'
// 下拉组件
import carSourceFilter from '@/components/down-filters/car-source-filter.vue'
import carGoodsFilter from '@/components/down-filters/car-goods-filter.vue'
import regiog from '@/components/down-filters/region-list'
import findCar from '@/components/down-filters/find-car-filter'
import accessory from '@/components/down-filters/accessory-filter'
import storeFilter from './components/store-car-filter.vue' //美护宝列表
export default {
  components: {
    carSourceCard,
    beautyCard,
    findPj,
    storeList,
    carSourceFilter,
    carGoodsFilter,
    regiog,
    findCar,
    accessory,
    storeFilter
  },
  data() {
    return {
      pageLoading: false,
      stickyHeight: -2,
      hHeight: 44,
      tabTop: false, // tab吸顶时触发的开关
      storeId: '', //店铺id
      goodsList: [],
      priceId: '', //价格排序id
      typeId: '', //商家类型id
      carId: '', //车源id
      addId: '', //车源地址id
      //搜索的值
      search: '',
      //下拉搜索类型
      searchTypeMap: new Map([
        [
          'CarStore',
          [{
            'name': '车品',
            'api_url': ''
          },
          {
            'name': '店铺',
            'api_url': 'https://developapi.yzsheng.com/bussiness/Index/SearchStore'
          },
          {
            'name': '车源',
            'api_url': 'https://developapi.yzsheng.com/bussiness/Index/SearchCar'
          },

          {
            'name': '配件',
            'api_url': ''
          },
          {
            'name': '寻车',
            'api_url': ''
          },
          ],
        ],

        [
          'Factory',
          [{
            'name': '店铺',
            'api_url': 'https://developapi.yzsheng.com/bussiness/Index/SearchStore'
          }, {
            'name': '寻车',
            'api_url': ''
          }],
        ],

        [
          'BeautyStore',
          [{
            'name': '车品',
            'api_url': 'https://developapi.yzsheng.com/bussiness/Index/SearchStore'
          },
          {
            'name': '店铺',
            'api_url': ''
          }
          ]
        ],

        [
          'RepairStore',
          [{
            'name': '配件',
            'api_url': ''
          },
          {
            'name': '店铺',
            'api_url': 'https://developapi.yzsheng.com/bussiness/Index/SearchStore'
          }
          ]
        ]
      ]),
      //车源查询
      selecCarValue: '',
      // 店铺筛选
      Car: [{
        label: '不限',
        value: 0
      },
      {
        label: '距离最近',
        value: 1
      },
      {
        label: '粉丝最多',
        value: 2
      }
      ],
      selectStore: [], //搜索店铺列表

      selectCarList: [], //车源列表
      //美护宝价格
      CoodPriceValue: 0,
      //美护宝销量
      coodList: [], //美护宝列表
      coodNumValue: '',
      //菜单类型
      text: '', //搜索值
      show: false,
      value: '', //下拉值
      listShow: false, //下拉显示

      searchValue: [], //搜索历史值列表
      isHistoryShow: false, //默认显示历史列表
      //下拉选项
      select: [{
        name: '车品',
        id: 0,
      },
      {
        name: '店铺',
        id: 1,
      },
      {
        name: '车源',
        id: 2,
      },
      {
        name: '寻车',
        id: 3,
      },
      ],

      latitude: '', //纬度
      longitude: '', //经度

      findAddId: '', //寻车地址id
      newCarList: [], //寻车列表
      pjList: [],//配件列表
      storeAddressId: '', //店铺地址id
      //店铺筛选值
      selecStoreValue: '',
      // 懒加载
      status: '',
      //车源懒加载
      CarPageIndex: 1, //车源当前页
      //店铺懒加载
      storePageIndex: 1, //店铺当前页

      //美护宝懒加载
      coodPageIndex: 1, //车品当前页
      newCarIndex: 1,//寻车当前页
      pjIndex: 1,//配件当前页
      //寻配件筛选
      accessoryBid: '', //寻配件品牌id
      accessoryType: '', //寻配件类型
      accessoryRegionId: '', //寻配件地区id
      accessoryOrderBy: 0, //寻配排序

      //最新寻车筛选
      findCarVersion: 0, // 寻车类型
      findCarBid: '', // 寻车品牌id
      findCarAddressId: '', // 寻车地址id

      carGoodsCategoryList: [],
    }
  },
  //加载数据
  onReachBottom() {
    if (this.value === '车源') {
      this.searchCar()
    } else if (this.value === '车品') {
      this.searchCood()
    } else if (this.value === '店铺') {
      this.searchStore()
    } else if(this.value === '配件') {
      this.getCarPartListPaged()
    } else if(this.value === '寻车') {
      this.getCarFindBList()
    }
  },
  onLoad(){

  },
  onReady() {
    // //获取导航栏高度
    // const query = uni.createSelectorQuery().in(this)
    // query.select('.nav-s').boundingClientRect(data => {
    //   console.log(data.height)
    //   // this.stickyHeight = data.height
    //   this.stickyHeight = Math.floor(Number(data.height) / (uni.upx2px(Number(data.height)) / Number(data.height))) - 1
    // }).exec()
  },
  created() {
    // this.stickyHeight = uni.getSystemInfoSync().statusBarHeight
    let auth = this.$store.state.user.userInfo.permious
    // CarStore
    this.select = this.searchTypeMap.get('CarStore')
    this.value = this.select[0].name
    this.getCategoryList()
    if (this.value === '车源') {
      this.searchCar()
    } else if (this.value === '车品') {
      console.log('美护宝')
      this.searchCood()
    } else if (this.value === '店铺') {
      // this.searchStore()
    } else if (this.value === '配件') {
      this.getCarPartListPaged()
    } else if (this.value === '寻车') {
      this.getCarFindBList()
    }
  },
  computed: {
    // stickyHeights() {
    //   return Math.floor(Number(this.stickyHeight) / (uni.upx2px(Number(this.stickyHeight)) / Number(this.stickyHeight))) - 1
    // }
  },
  onLoad() {
    console.log(this.$store.state.userInfoDetail.store.storeId, '11')
    this.storeId = this.$store.state.userInfoDetail.store.storeId
    //历史
    uni.getStorage({
      key: 'searchLocal',
      success: res => {
        if (res.data !== '') {
          console.log(JSON.parse(res.data))
          this.searchValue = JSON.parse(res.data)
        }
      },
      fail: err => {
        console.log(err)
      }
    })
    this.latitude = this.$store.state.latitude
  },
  methods: {
    //加载更多
    storeMore(){
      this.searchStore()
    },
    coodMore(){
      this.searchCood()
    },
    careMore(){
      this.searchCar()
    },
    newCarMore(){
      this.getCarFindBList()
    },
    pjMore(){
      this.getCarPartListPaged()
    },

    filterStoreListChange({
		  type,
		  id
    }) {
		  // type: 0是品牌，1是地区，2是商家类型
		  switch (type) {
		  case 0:

		    console.log('地区')
        this.storeAddressId = id
		    break
		  case 1:
        this.selecCarValue = id
		    console.log('筛选')
		    break
		  }
      console.log(type)
		  this.selectStore = []
		  this.storePageIndex = 1
		  this.searchStore()
    },

    open(e){
      console.log(e)
    },
    close(e){

    },
    // 获取分类列表
    async getCategoryList() {
		  try {
		    let data = {
		      pageIndex: 1,
		      pageSize: 100,
		    }
		    const res = await this.$api.carGoods.GetGoodsCarSupplieTypeListPaged(
		      data
		    )
        // console.log(res,'1')
		    this.carGoodsCategoryList = res.data.list.map(item => ({
		      label: item.name,
		      value: item.id,
		    }))
		  } catch (e) {
		    //
		  }
    },
    //监听搜索框
    changes(e){
      if(e.detail.value===''){
				 this.text = ''
				 if (this.value === '车源') {
				   this.selectCarList = []
				   this.CarPageIndex = 1
					 this.$u.debounce(this.searchCar, 1000)
				 } else if (this.value === '车品') {
				   this.coodList = []
				   this.coodPageIndex = 1
					 this.$u.debounce(this.searchCood, 1000)
				   // this.searchCood()
				 } else if (this.value === '店铺') {
				   this.selectStore = []
				   this.storePageIndex = 1
					 this.$u.debounce(this.searchStore, 1000)
				   // this.searchStore()
				 } else if (this.value === '配件') {
				   this.pjList = []
				   this.pjIndex = 1
					 this.$u.debounce(this.getCarPartListPaged, 1000)
				   // this.getCarPartListPaged()
				 } else if (this.value === '寻车') {
				   this.newCarList = []
				   this.newCarIndex = 1
					 this.$u.debounce(this.getCarFindBList, 1000)
				   // this.getCarFindBList()
				 }
      }
    },
    //下拉选中
    choice(value) {
      this.listShow = false
      this.value = value
      console.log(value, '值')
      if (value === '车源') {
        this.CarPageIndex = 1
        this.selectCarList = []
        this.text=''
        // this.carId='' //车型id
        // this.addId='' //地区id
        // this.typeId='' //筛选条件
        // this.priceId=''//价格排序
        this.searchCar()
      } else if (value === '车品') {
        // this.coodPageIndex = 1
        // this.coodList = []
        this.text=''
        this.searchCood()
        console.log('美护宝')
      } else if (value === '店铺') {
        this.text=''
        // this.searchStore()
      } else if (value === '寻车') {
        this.text=''
        this.getCarFindBList()
      } else if(value === '配件'){
        this.text=''
        this.getCarPartListPaged()
      }

    },
    // 加载前、中、后
    moreStatus(status) {
      this.status = status
    },
    //搜索车品
    async searchCood() {
      this.status = 'loading'
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        let data = {
          'PageIndex': this.coodPageIndex,
          'PageSize': 10,
          // 'SearchKey': this.text,
          // 'PriceOrder': this.CoodPriceValue,
          // 'SoldOrder': this.coodNumValue,
          'content': this.text,
          'type_Id': this.coodNumValue,
          'sorting': this.CoodPriceValue
        }
        const res = await this.$api.common.indexSearchCood(data)
        this.isHistoryShow = false
        this.coodList = [...this.coodList, ...res.data.list]
        this.$nextTick(() => {
				  this.pageLoading = true
        })
        // this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        if (res.data.total <= 10) {
				  this.status = 'nomore'
        } else {
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.coodPageIndex += 1 // 当前页加1
      } catch (e) {

      }
    },
    //搜索车型
    async searchCar() {
      this.status = 'loading'
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        let data = {
          'PageIndex': this.CarPageIndex,
          'PageSize': 10,
          'SearchKey': this.text,
          'CarId': this.carId, //车型id
          'RegionId': this.addId, //地区id
          'StoreType': this.typeId !== 1 ? this.typeId : '', //筛选条件
          'OrderType': this.priceId //价格排序
        }
        const res = await this.$api.common.indexSearchCar(data)
        this.isHistoryShow = false
        this.selectCarList = [...this.selectCarList, ...res.data.list]
        this.$nextTick(() => {
          this.pageLoading = true
        })
        if (res.data.total <= 10) {
				  this.status = 'nomore'
        } else {
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.CarPageIndex += 1 // 当前页加1
      } catch (e) {

      }
    },

    //搜索店铺
    async searchStore() {
      this.status = 'loading'
      uni.showLoading({
      	title: '加载中',
      	mask: true
      })
      try {
        let latitude = this.$store.state.latitude //维度
        let longitude = this.$store.state.longitude //经度
        let data = {
          'PageIndex': this.storePageIndex,
          'PageSize': 10,
          'SearchKey': this.text, //搜索关键字
          'RegionId': this.storeAddressId, //地区id
          'Screen': this.selecCarValue, //筛选(0、不限 1、距离最近 2、粉丝最多)
          'lat': latitude ? latitude : 104.063364, //经度
          'lon': longitude ? longitude : 30.566, //纬度
        }
        const res = await this.$api.common.indexSearchStore(data)

        this.isHistoryShow = false
        this.selectStore = [...this.selectStore, ...res.data.list]
        this.$nextTick(() => {
          this.pageLoading = true
        })
        // this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        if (res.data.total <= 10) {
				  this.status = 'nomore'
        } else {
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.storePageIndex += 1 // 当前页加1
      } catch (e) {

      }
    },

    //寻车
    async getCarFindBList() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      this.status = 'loading'
      try {
        let data = {
          'pageIndex': this.newCarIndex,
          'pageSize': 10,
          'search': this.text,
          'brand': this.findCarBid,
          'specification': this.findCarVersion,
          'region_Limit_Id': this.findCarAddressId,
          'store_Id': this.storeId
        }
        const res = await this.$api.common.getCarFindBList(data)

        this.isHistoryShow = false
        this.newCarList = [...this.newCarList, ...res.data.list]
        this.$nextTick(() => {
          this.pageLoading = true
        })
        if (res.data.total <= 10) {
				  this.status = 'nomore'
        } else {
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.newCarIndex += 1 // 当前页加1
      } catch (e) {

      }
    },

    //配件
    async getCarPartListPaged() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      let latitude = this.$store.state.latitude //维度
      let longitude = this.$store.state.longitude //经度
      this.status = 'loading'
      try {
        let data = {
          pageSize: 5,
          pageIndex: this.pjIndex,  //页码
          keyWords: this.text, //搜索关键字
          brandId: this.accessoryBid, //搜索品牌
          partCategoryId: this.accessoryType, //配件类目id
          baseRegionId: this.accessoryRegionId, //市级id
          lat: latitude ? latitude : 104.063364, //经度
          lon: longitude ? longitude : 30.566, //纬度
          sort: this.accessoryOrderBy,
        }
        const res = await this.$api.common.getCarPartListPaged(data)
        this.isHistoryShow = false
        this.pjList = [...this.pjList, ...res.data.list]
        this.$nextTick(() => {
          this.pageLoading = true
        })
        if (res.data.total <= 10) {
				  this.status = 'nomore'
        } else {
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.pjIndex += 1 // 当前页加1

      } catch (e) {
        uni.hideLoading()
      }
    },
    // 车源列表下拉筛选
    filterCarListChange({
      type,
      id
    }) {
      // type: 0是品牌，1是地区，2是商家类型
      switch (type) {
      case 0:
        this.carId = id
        console.log('品牌')
        break
      case 1:
        this.addId = id
        console.log('地址')
        break
      case 2:
        this.typeId = id[0]
        this.priceId = id[1]
        break
      }
      this.selectCarList = []
      this.CarPageIndex = 1
      this.searchCar()
    },

    // 筛选车品列表
    filterCarGoodsListChange({
      type,
      id
    }) {
      // type: 0是分类，1是排序
      switch (type) {
      case 0:
        this.coodNumValue = id
        break
      case 1:
        this.CoodPriceValue = id
        break
      }
      this.coodList = []
      this.coodPageIndex = 1
      this.searchCood()
    },

    //筛选寻车列表
    filterFindCarListChange({
      type,
      id
    }) {
      // type: 0是品牌，1是全部类型，2是地区
      switch (type) {
      case 0:
        this.findCarBid = id
        break
      case 1:
        this.findCarVersion = id
        console.log(id)
        break
      case 2:
        this.findCarAddressId = id
        break
      }
      this.newCarList = []
      this.newCarIndex = 1
      this.getCarFindBList()

    },

    //筛选寻配件列表
    accessoryFilterListChange({
      type,
      id
    }) {
      // type: 0是品牌，1是全部类型，2是地区，3是排序
      switch (type) {
      case 0:
        this.accessoryBid = id + ''
        break
      case 1:
        this.accessoryType = id ? id + '' : ''
        break
      case 2:
        this.accessoryRegionId = id
        break
      case 3:
        this.accessoryOrderBy = id
        break
      }
      this.pjList = []
      this.pjIndex = 1
      this.getCarPartListPaged()
    },
    // 店铺-----------------------------------------------------------

    //店铺筛选
    selecStore(e) {
      this.selectStore = []
      this.storePageIndex = 1
      this.selecStoreValue = e
      this.searchStore()
    },

    // 店铺关闭弹出列表的部分
    closePopupSection() {
      this.$refs.storeuDropdown.close()
    },
    // 店铺选择地区
    regionChange(id) {
      console.log(id)
      this.selectStore = []
      this.storePageIndex = 1
      this.storeAddressId = id
      this.searchStore()
      this.$refs.storeuDropdown.close()
    },

    //跳转车源详情
    goCar(e) {
      console.log(e)
      console.log(e)
      uni.navigateTo({
        url: '/pages_carSource/productDetail/productDetail?goodsid=' + e,
      })
    },

    //显示历史记录
    onfocusStatus() {
      this.isHistoryShow = true
    },
    //历史事件
    storageList(item) {
      console.log(item, '历史')
      this.text = item
      //请求搜索接口
      if (this.value === '车源') {
        this.selectCarList = []
        this.CarPageIndex = 1
        this.searchCar()
      } else if (this.value === '车品') {
        this.coodList = []
        this.coodPageIndex = 1
        this.searchCood()
      } else if (this.value === '店铺') {
        this.selectStore = []
        this.storePageIndex = 1
        this.searchStore()
      } else if (this.value === '配件') {
        this.pjList = []
        this.pjIndex = 1
        this.getCarPartListPaged()
      } else if (this.value === '寻车') {
        this.newCarList = []
        this.newCarIndex = 1
        this.getCarFindBList()
      }
    },

    // 下拉显示
    Show() {
      console.log('1')
      this.listShow = !this.listShow
    },
    // 关闭遮罩层
    clickType() {
      this.listShow = false
    },
    //搜索
    onSearch(e) {
      console.log(e.target.value)
      // this.search = e.target.value
      this.text = e.target.value
      if (this.text === '') {
        uni.showToast({
          title: '请输入关键字',
          icon: 'none',
          duration: 1000
        })
        return false
      }
      let that = this
      let newArr = that.searchValue
      //如果有搜索相同的话，先删除，再添加新的
      if (this.searchValue.indexOf(this.text) === -1) {
        newArr.unshift(this.text)
      }
      //超过多少个就删除最后一个
      if (newArr.length >= 10) {
        newArr.pop()
      }
      newArr = JSON.stringify(newArr) //转换成json格式的数据
      uni.setStorage({ //将搜索的值添加到本地缓存
        key: 'searchLocal',
        data: newArr
      })
      //请求搜索接口
      if (this.value === '车源') {
        this.selectCarList = []
        this.CarPageIndex = 1
        this.$u.debounce(this.searchCar, 1000)
        // this.searchCar()
      } else if (this.value === '车品') {
        this.coodList = []
        this.coodPageIndex = 1
        this.$u.debounce(this.searchCood, 1000)
        // this.searchCood()
      } else if (this.value === '店铺') {
        this.selectStore = []
        this.storePageIndex = 1
        // this.searchStore()
        this.$u.debounce(this.searchStore, 1000)
      } else if (this.value === '配件') {
        this.pjList = []
        this.pjIndex = 1
        // this.getCarPartListPaged()
        this.$u.debounce(this.getCarPartListPaged, 1000)
      } else if (this.value === '寻车') {
        this.newCarList = []
        this.newCarIndex = 1
        // this.getCarFindBList()
        this.$u.debounce(this.getCarFindBList, 1000)
      }
    },

    //清楚缓存
    del() {
      console.log('del')
      var that = this
      uni.setStorage({
        key: 'searchLocal',
        data: that.searchValue
      })
      this.searchValue = []
    },
  }
}
